package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.banner.model.AddBannerModel;
import com.ls.controller.banner.model.EditBannerModel;
import com.ls.controller.banner.model.SortBannerModel;
import com.ls.controller.benefit.model.SortBenefitModel;
import com.ls.controller.product.model.AddProductModel;
import com.ls.entity.Banner;
import com.ls.entity.Benefit;
import com.ls.entity.Product;
import com.ls.entity.SysUser;
import com.ls.enums.RoleEnum;
import com.ls.mapper.BannerMapper;
import com.ls.service.IBannerService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
@RequiredArgsConstructor
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {
    /** 新增轮播图 */
    @Override
    public ResultData add(AddBannerModel model) {
        SysUser sysUser  = SaTokenUtilPy.getSysUser();
        RoleEnum roleEnum = RoleEnum.getRoleEnum(sysUser.getRoleId());
        if (!RoleEnum.ADMIN.equals(roleEnum)) {
            return ResultData.failure("只有管理员才能新增权益");
        }
        //实现新增轮播图
        Banner banner = new Banner();
        BeanUtils.copyProperties(model, banner);
        long count = count();
        banner.setSort((int) (count+1));
        boolean flag = save(banner);
        if (flag){
            return ResultData.successMsg("新增成功");
        }else {
            return ResultData.failure("新增失败");
        }
    }
    /** 编辑轮播图 */
    @Override
    public ResultData edit(EditBannerModel model) {
        SysUser sysUser  = SaTokenUtilPy.getSysUser();
        RoleEnum roleEnum = RoleEnum.getRoleEnum(sysUser.getRoleId());
        if (!RoleEnum.ADMIN.equals(roleEnum)) {
            return ResultData.failure("只有管理员才能修改轮播");
        }
        //获取当前轮播图id判断是否存在
        Banner banner = getById(model.getBannerId());
        if (banner == null){
            return ResultData.failure("轮播图不存在");
        }
        //实现修改轮播图
        BeanUtils.copyProperties(model, banner);
        banner.setUpdateTime(LocalDateTime.now());
        boolean flag = updateById(banner);
        if (flag){
            return ResultData.successMsg("修改成功");
        }else {
            return ResultData.failure("修改失败");
        }
    }
    /** 轮播图排序 */
    @Override
    public ResultData sort(SortBannerModel model) {
        SysUser sysUser  = SaTokenUtilPy.getSysUser();
        RoleEnum roleEnum = RoleEnum.getRoleEnum(sysUser.getRoleId());
        if (!RoleEnum.ADMIN.equals(roleEnum)) {
            return ResultData.failure("只有管理员才能修改轮播图");
        }
        //获取当前权益id判断是否存在
        Banner banner = getById(model.getBannerId());
        if (banner == null){
            return ResultData.failure("轮播图不存在");
        }
        //实现修改权益
        BeanUtils.copyProperties(model, banner);
        banner.setUpdateTime(LocalDateTime.now());
        boolean flag = updateById(banner);
        if (flag){
            return ResultData.successMsg("修改成功");
        }else {
            return ResultData.failure("修改失败");
        }
    }
    /** 查询轮播图列表 */
    @Override
    public ResultData getBanner() {
        return ResultData.successData(list(new LambdaQueryWrapper<Banner>().orderByAsc(Banner::getSort)));

    }
    /** 删除轮播图 */
    @Override
    public ResultData del(Integer bannerId) {
        Banner banner = getById(bannerId);
        if (banner == null){
            return ResultData.failure("轮播图不存在");
        }
        return banner.deleteById()?ResultData.successMsg("删除成功"):ResultData.failure("删除失败");
    }
}
